home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 1992 August
/
info-mac-1992.iso
/
UNIX
/
Mcvert 1.83.shar
< prev
next >
Wrap
Text File
|
1992-08-29
|
75KB
|
2,387 lines
10-Aug-92 20:30:19-GMT,75337;000000000201
Received: from SUMEX-AIM.Stanford.EDU by CAMIS.Stanford.EDU (4.1/inc-1.0)
id AA15860; Mon, 10 Aug 92 13:30:14 PDT
Full-Name: Info-Mac Moderator
Received: by SUMEX-AIM.Stanford.EDU (4.1/inc-1.0)
id AA11539; Mon, 10 Aug 92 13:30:09 PDT
Resent-Message-Id: <9208102030.AA11539@SUMEX-AIM.Stanford.EDU>
Return-Path: <mntgfx!std.MENTORG.COM!jskudlarek@uunet.UU.NET>
Received: from relay2.UU.NET by CAMIS.Stanford.EDU (4.1/inc-1.0) id AA13868;
Mon, 10 Aug 92 12:05:16 PDT
Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP
(5.61/UUNET-internet-primary) id AA23142; Mon, 10 Aug 92 15:03:49
-0400
Received: from mntgfx.UUCP by uunet.uu.net with UUCP/RMAIL (queueing-rmail)
id 145834.2213; Mon, 10 Aug 1992 14:58:34 EDT
Received: by pdx.MENTORG.COM ( 5.52 (84)/DontTreadOnMe) id AA21558; Mon, 10
Aug 92 10:38:47 PDT
Received: by std.MENTORG.COM (5.65c/smail2.5/09-24-87/Mentor) id AA16473;
Mon, 10 Aug 1992 10:40:51 -0700
Date: Mon, 10 Aug 1992 10:40:51 -0700
From: jskudlarek@std.MENTORG.COM (Joseph Skudlarek)
Message-Id: <199208101740.AA16473@std.MENTORG.COM>
To: info-mac@sumex-aim.stanford.edu
Subject: mcvert-183.shar
Resent-To: backmod@SUMEX-AIM.Stanford.EDU
Resent-Date: Mon, 10 Aug 1992 13:30:03 PDT
Resent-From: Info-Mac Moderator <macmod@sumex-aim.Stanford.EDU>
mcvert, written in C, runs under UNIX and converts between Macintosh
file formats including BinHex4.0 and MacBinary. See mcvert.1 (the man
page) for details.
Please replace mcvert-182.shar with mcvert-183.shar, which follows.
The the current usage and the additional changes since 1.82 are
detailed below.
Usage: mcvert { [options] name ...} ...
default:
-xDqv
option:
-x BinHex .hqx <-> MacBinary
-r Resource .rsrc <-> MacBinary
-d Data .data <-> MacBinary
-u Text(trans) .text <-> MacBinary
-h Host(as is) .text <-> MacBinary
-D Download (Other -> MacBinary)
-U Upload (MacBinary -> Other)
-p BinHex -> MacBinary => unpack PIT
-q disable unpack PIT
-s silent
-S Silent about ``Converting ... '' lines too
-v verbose
-V Verbose, includes debugging information
Environment:
MAC_EDITOR MACA Mac creator for text files
MAC_DLOAD_DIR . directory for -D files
MAC_EXT .bin extension for -D files
MAC_LINE_LIMIT none maximum line length for -Ux files
* ----------------------------------------------------------------------------
* External
* -----
* relax exactly 64 characters per incoming hqx file, and
* handle files without trailing newline, so, eg,
* Telnet2.5docs.sit.hqx now converts correctly (failure
* reported by Justin Sullivan <justin@f2.facts.uky.edu>)
* now also processes info-mac/app/road-map.hqx correctly
* (failure reported by Victor Norton<norton@andy.bgsu.edu>)
* rework the man page for improved clarity and completeness
* -----
* Internal
* -----
* avoid warning message from gcc on Sequent Balance mainframe
* reported by Justin Sullivan <justin@f2.facts.uky.edu>
* bump max incoming line length to 2048 from 255
* add mcvert.ps target to Makefile
* -----
* Joseph Skudlarek Mentor Graphics 8005 SW Boeckman Rd Wilsonville OR 97070
* (503) 685-1576 (work)
* {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
* ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
* Version 1.83 03Aug92
* ----------------------------------------------------------------------------
#!/bin/sh
# This is a shell archive (shar 3.47)
# made 08/10/1992 17:34 UTC by jskudlarek@hal
# Source directory //hal/local_user/jskudlarek/notes/info-mac/mcvert
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 19826 -r--r--r-- mcvert.c
# 23244 -r--r--r-- hqxify.c
# 6096 -r--r--r-- unpack.c
# 9603 -r--r--r-- mactypes.h
# 1108 -r--r--r-- Makefile
# 7376 -r--r--r-- mcvert.1
#
# ============= mcvert.c ==============
if test -f 'mcvert.c' -a X"$1" != X"-c"; then
echo 'x - skipping mcvert.c (File already exists)'
else
echo 'x - extracting mcvert.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mcvert.c' &&
/**
X * mcvert.c - version 1.05 - 10 January, 1990
X * Written by Doug Moore - Rice University - dougm@rice.edu - April '87
X
X * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
X
X * Changed default max_line_size from 2000 to unlimited - Doug Moore, April, '89
X
X * Sun 3/60 doesn't like odd-sized structs. Bug fixed - Doug Moore, April, '89
X * - aided by Spencer W. Thomas
X
X * Didn't handle properly many hqx files combined in one file. Bug fixed -
X * Doug Moore, June, '89
X
X * Modified to handle MacBinaryII specification. Jim Van Verth, Sept, '89
X
X * Fixed a bug when there are blank lines in hqx data, as happens when newline
X * get translated to CRLF and then to \n\n, common for some file transfers.
X * The last hqx line would be lost if the previous line was blank or junk.
X * Glenn Trewitt, Stanford University, 1990 (1.05)
X
X * Fixed a bug that occurred when extracting data or resource
X * forks on a Sun 4. It was a byte alignment problem.
X * Rick Zaccone, zaccone@bucknell.edu. April 1991. Version 1.6
X
X * Fixed:
X * Sent all "Converting ... " lines to stdout instead of stderr
X * Changed mactypes.h for HP-UX systems
X * Alan Danziger, aland@cs.brandeis.edu. October 1991. Version 1.6.5
X
X * ----------------------------------------------------------------------------
X * External
X * -----
X * Fixed buffering bug when converting very small MacBinary files to hqx files.
X * Provide helpful usage line.
X * Control "Converting ... " lines separately with -S flag.
X * Make encoding and decoding consistent by ignoring locked and init flags.
X * Clean up some error messages; check for more errors; provide errno on error.
X * Updated the man page.
X * -----
X * Internal
X * -----
X * Reformat source (sorry, local standard used by tools is tab space == 3)
X * Remove compiler warning messages.
X * Rename some variables.
X * Added some comments to code.
X * Added some offsets to struct definitions.
X * Since the makefile has compilation flags,
X * make the compiles depend on the Makefile.
X * -----
X * Thanks to all who have gone before for creating, maintaining,
X * improving, and providing this program and documentation.
X * -----
X * Joseph Skudlarek Mentor Graphics 8005 SW Boeckman Rd Wilsonville OR 97070
X * {Jskud@std.mentorg.com,Joseph_Skudlarek@mentorg.com}
X * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
X * Version 1.70 09Jul92
X * ----------------------------------------------------------------------------
X
X * ----------------------------------------------------------------------------
X * External
X * -----
X * Added -V (Verbose) option (includes debugging information)
X * Fixed bug converting hqx to MacBinary if last line is ":"
X * Avoided a silent error and quick exit situation.
X * -----
X * Internal
X * -----
X * Got rid of almost all lint (SunOS and HP-UX) error messages.
X * Compiled on SunOs, HP-UX, DomainOS.
X * Incorporated Parag Patel <parag@netcom.com> changes for AU/X.
X * Here's some diffs for really quick cheap hacks to get mcvert to compile
X * and run under A/UX. The main problem was that timeb does not exist, so
X * I added 2 #ifdef TIMEVAL to use the System-V timeval package instead.
X * The Makefile just has a -DTIMEVAL and a magic -U_SYSV_SOURCE to get
X * around a pre-defined type "ulong" in sys/types.h (thanks to Apple).
X * Did more code overhauling:
X * add lots more comments, rename variables, reformat source.
X * Put code in un_hqx to avoid suspected buffering problem.
X * -----
X * Joseph Skudlarek Mentor Graphics 8005 SW Boeckman Rd Wilsonville OR 97070
X * (503) 685-1576 (work)
X * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
X * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
X * Version 1.80 15Jul92
X * ----------------------------------------------------------------------------
X
X * ----------------------------------------------------------------------------
X * External
X * -----
X * Made hqx file scan processing much smarter
X * so, for example, info-mac/comm/qwk-reader.hqx,
X * complete with extraneous colons in column one, converts correctly
X * (problem described by Edward John Sabol <es2j+@andrew.cmu.edu>)
X * Avoid silly perror on usage message (prompted by Edward John Sabol)
X * Improve error message regarding improper format
X * Added more caveats to man page
X * -----
X * Internal
X * -----
X * Fixed typo's in printf lines to pass all expected arguments
X * (pointed out by Bo Holst-Christensen
X * [holst@diku.dk/dikubhc1@uts.uni-c.dk/holst@login.dkuug.dk])
X * Tweak Makefile to ease shar creation and special case ulong, not A/UX
X * Add yet more comments and debugging code
X * -----
X * Joseph Skudlarek Mentor Graphics 8005 SW Boeckman Rd Wilsonville OR 97070
X * (503) 685-1576 (work)
X * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
X * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
X * Version 1.82 30Jul92
X * ----------------------------------------------------------------------------
X
X * ----------------------------------------------------------------------------
X * External
X * -----
X * relax exactly 64 characters per incoming hqx file, and
X * handle files without trailing newline, so, eg,
X * Telnet2.5docs.sit.hqx now converts correctly (failure
X * reported by Justin Sullivan <justin@f2.facts.uky.edu>)
X * now also processes info-mac/app/road-map.hqx correctly
X * (failure reported by Victor Norton<norton@andy.bgsu.edu>)
X * rework the man page for improved clarity and completeness
X * -----
X * Internal
X * -----
X * avoid warning message from gcc on Sequent Balance mainframe
X * reported by Justin Sullivan <justin@f2.facts.uky.edu>
X * bump max incoming line length to 2048 from 255
X * add mcvert.ps target to Makefile
X * -----
X * Joseph Skudlarek Mentor Graphics 8005 SW Boeckman Rd Wilsonville OR 97070
X * (503) 685-1576 (work)
X * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
X * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
X * Version 1.83 03Aug92
X * ----------------------------------------------------------------------------
X
X * ----------------------------------------------------------------------------
X * This program may be freely distributed for non-profit purposes. It may not
X * be sold, by itself or as part of a collection of software. It may be freely
X * modified as long as no modified version is distributed. Modifications of
X * interest to all can be incorporated into the program by sending them to me
X * for distribution. Parts of the code can be used in other programs. I am not
X * responsible for any damage caused by this program. I hope you enjoy it.
X *
X * ----------
X * Things that yet could be done:
X * provide "verify only" mode which would read and report but
X * not write any files -- useful for verifying formats
X * and for finding out the names of the files which
X * would be created.
X * ----------
X */
X
/*
X * Naming
X * DOWNLOAD
X * => converting TO MacBinary
X * => direction == FORWARDS
X * => use un_* routines (un => UNdo encoding?)
X * UPLOAD
X * => converting FROM MacBinary
X * => direction == BACKWARDS
X * => use re_* routines (re => Really Encode?)
X */
X
#include "mactypes.h"
X
#define HQX 0
#define TEXT 1
#define DATA 2
#define RSRC 3
#define HOST 4
#define FORWARDS 0
#define BACKWARDS 1
X
FILE *verbose;
FILE *debug;
FILE *convert;
FILE *devnull;
FILE *output;
X
char **hqxnames, **hqxnames_left;
char *dir, *ext, *text_author;
char *maxlines_str;
int maxlines;
X
char Usage[] = "\
Usage: %s { [options] name ...} ...\n\
X default:\n\
\t-xDqv\n\
\n\
X option:\n\
\t-x\tBinHex .hqx \t<-> MacBinary\n\
\t-r\tResource .rsrc\t<-> MacBinary\n\
\t-d\tData .data\t<-> MacBinary\n\
\t-u\tText(trans) .text\t<-> MacBinary\n\
\t-h\tHost(as is) .text\t<-> MacBinary\n\
\n\
\t-D\tDownload (Other -> MacBinary)\n\
\t-U\tUpload (MacBinary -> Other)\n\
\n\
\t-p\tBinHex -> MacBinary => unpack PIT\n\
\t-q\tdisable unpack PIT\n\
\n\
\t-s\tsilent\n\
\t-S\tSilent about ``Converting ... '' lines too\n\
\t-v\tverbose\n\
\t-V\tVerbose, includes debugging information\n\
\n\
Environment:\n\
\tMAC_EDITOR \tMACA\tMac creator for text files\n\
\tMAC_DLOAD_DIR \t. \tdirectory for -D files\n\
\tMAC_EXT \t.bin\textension for -D files\n\
\tMAC_LINE_LIMIT\tnone\tmaximum line length for -Ux files\n\
";
X
char *cmdname;
X
main(argc, argv)
X int argc;
X char **argv;
{
X char *flags, *getenv();
X int direction, mode, unpit_flag;
X
X cmdname = argv[0];
X
X /* Early error and clean exit if missing arguments */
X if (argc < 2) {
X usage();
X /*NOTREACHED*/
X }
X
X devnull = fopen("/dev/null", "w");
X
X argv++;
X argc--;
X verbose = stderr;
X debug = devnull;
X output = stdout;
X convert = stdout;
X direction = FORWARDS;
X mode = HQX;
X unpit_flag = 0;
X
X if ((text_author = getenv("MAC_EDITOR")) == NULL)
X text_author = "MACA";
X if ((ext = getenv("MAC_EXT")) == NULL)
X ext = ".bin";
X if ((dir = getenv("MAC_DLOAD_DIR")) == NULL)
X dir = ".";
X if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)
X maxlines = 0;
X else {
X maxlines = atoi(maxlines_str);
X if (maxlines < MIN_HQX_LINES) {
X fprintf(stderr, "%s: %s; was %d; reset to %d\n",
X cmdname,
X "warning: MAC_LINE_LIMIT too small",
X maxlines, MIN_HQX_LINES);
X maxlines = MIN_HQX_LINES;
X }
X }
X
X /* Make command line arguments globally accessible */
X hqxnames = (char **) calloc((unsigned)argc + 1, sizeof(char *));
X hqxnames_left = hqxnames;
X while (argc--)
X *hqxnames_left++ = *argv++;
X
X /* Flag the end of the list */
X *hqxnames_left = "-";
X hqxnames_left = hqxnames;
X
X /* While not at the end of the list */
X while (strcmp(*hqxnames_left, "-")) {
X if (hqxnames_left[0][0] == '-') {
X flags = *hqxnames_left++;
X while (*++flags)
X switch (*flags) {
X case 'x':
X mode = HQX;
X break;
X case 'u':
X mode = TEXT;
X break;
X case 'd':
X mode = DATA;
X break;
X case 'r':
X mode = RSRC;
X break;
X case 'h':
X mode = HOST;
X break;
X case 'D':
X direction = FORWARDS;
X break;
X case 'U':
X direction = BACKWARDS;
X break;
X case 'q':
X unpit_flag = 0;
X break;
X case 'p':
X unpit_flag = 1;
X break;
X case 'S':
X convert = devnull;
X /* FALLTHRU */
X case 's':
X verbose = devnull;
X break;
X case 'v':
X verbose = stderr;
X break;
X case 'V':
X debug = stderr;
X break;
X default:
X usage();
X /*NOTREACHED*/
X }
X }
X if (direction == BACKWARDS)
X if (mode == HQX && unpit_flag)
X re_hqx(); /* no re_pit() yet */
X else if (mode == HQX)
X re_hqx();
X else
X re_other(mode);
X else if (mode == HQX)
X un_hqx(unpit_flag);
X else
X un_other(mode);
X }
X
X exit(0);
X /*NOTREACHED*/
}
X
/* An array useful for CRC calculations that use 0x1021 as the "seed" */
word magic[] = {
X 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
X 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
X 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
X 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
X 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
X 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
X 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
X 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
X 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
X 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
X 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
X 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
X 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
X 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
X 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
X 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
X 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
X 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
X 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
X 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
X 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
X 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
X 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
X 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
X 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
X 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
X 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
X 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
X 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
X 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
X 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
X 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
X
X
/*
X * calc_crc() --
X * Compute the MacBinary II-style CRC for the data pointed to by p, with the
X * crc seeded to seed.
X *
X * Modified by Jim Van Verth to use the magic array for efficiency.
X */
short
calc_mb_crc(p, len, seed)
X unsigned char *p;
X long len;
X short seed;
{
X short hold; /* crc computed so far */
X long i; /* index into data */
X
X extern unsigned short magic[]; /* the magic array */
X
X hold = seed; /* start with seed */
X for (i = 0; i < len; i++, p++) {
X hold ^= (*p << 8);
X hold = (hold << 8) ^ magic[(unsigned char) (hold >> 8)];
X }
X
X return (hold);
} /* calc_crc() */
X
X
/* Report a fatal error */
error(msg, name)
X char msg[], name[];
X
{
X fprintf(stderr, msg, name);
X putc('\n', stderr);
X perror("\nlast perror (may not be relevent)");
X fprintf(stderr, "%s: exiting\n", cmdname);
X exit(2);
X /*NOTREACHED*/
}
X
/* replace illegal Unix characters in file name */
/* make sure host file name doesn't get truncated beyond recognition */
unixify(np)
X register char *np;
{
X register ulong c;
X
X c = strlen(np);
X if (c > SYSNAMELEN - 4)
X c = SYSNAMELEN - 4;
X np[c] = '\0';
X np--;
X while (c = *++np)
X if (c <= ' ' || c == '/' || c > '~')
X *np = '_';
}
X
/* Convert Unix time (GMT since 1-1-1970) to Mac time (local since 1-1-1904) */
#define MACTIMEDIFF 0x7c25b080 /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
X
X
ulong
time2mac(xtime)
X ulong xtime;
{
#ifdef TIMEVAL
X struct timeval t;
X struct timezone tz;
X
X gettimeofday(&t, &tz);
X return long2mac(xtime + MACTIMEDIFF
X - 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
#else
X struct timeb tp;
X
X ftime(&tp);
X return long2mac(xtime + MACTIMEDIFF
X - 60 * (tp.timezone - 60 * tp.dstflag));
#endif
}
X
/* This procedure copies the input file to the output file, basically, although
X in TEXT mode it changes LF's to CR's and in any mode it forges a Mac info
X header. Author type for TEXT mode can come from the MAC_EDITOR environ-
X ment variable if it is defined. */
X
un_other(mode)
X int mode;
{
X register ulong b;
X register ulong nchars;
X char txtfname[BINNAMELEN], binfname[BINNAMELEN];
X FILE *txtfile, *binfile;
X char *suffix;
X struct stat stbuf;
X info_header info;
X int extra_chars;
X ulong dlen, rlen, mtim, ctim;
X short crc, calc_mb_crc();
X
X if (mode == DATA)
X suffix = ".data";
X else if (mode == RSRC)
X suffix = ".rsrc";
X else
X suffix = ".text";
X
X while (hqxnames_left[0][0] != '-') {
X
X strcpy(txtfname, *hqxnames_left++);
X if (!(txtfile = fopen(txtfname, "r"))) {
X /*
X * Maybe we are supposed to figure out the suffix
X * ourselves?
X */
X strcat(txtfname, suffix);
X if (!(txtfile = fopen(txtfname, "r")))
X error("Cannot open %s", txtfname);
X }
X if (stat(txtfname, &stbuf))
X error("Cannot read %s", txtfname);
X
X /* stuff header data into the info header */
X bzero((char*)&info, sizeof(info_header));
X info.nlen = strlen(txtfname);
X info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
X info.name[info.nlen] = '\0';
X strcpy((char*)info.name, txtfname); /* name */
X mtim = time2mac((ulong)stbuf.st_mtime);
X ctim = time2mac((ulong)stbuf.st_ctime);
X bcopy((char*)&mtim, (char*)info.mtim, 4);
X bcopy((char*)&ctim, (char*)info.ctim, 4);
X info.uploadvers = '\201';
X info.readvers = '\201';
X
X if (mode == RSRC) {
X /* dlen is already zero */
X rlen = long2mac(stbuf.st_size);
X bcopy((char*)&rlen, (char*)info.rlen, 4);
X bcopy("APPL", (char*)info.type, 4);
X bcopy("CCOM", (char*)info.auth, 4);
X } else {
X dlen = long2mac(stbuf.st_size);
X bcopy((char*)&dlen, (char*)info.dlen, 4);
X /* rlen is already zero */
X bcopy("TEXT", (char*)info.type, 4);
X if (mode == DATA)
X bcopy("????", (char*)info.auth, 4);
X else
X bcopy(text_author, (char*)info.auth, 4);
X }
X
X /* calculate CRC */
X crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
X info.crc[0] = (char) (crc >> 8);
X info.crc[1] = (char) crc;
X
X /* Create the .bin file and write the info to it */
X sprintf(binfname, "%s/%s%s", dir, txtfname, ext);
X if ((binfile = fopen(binfname, "w")) == NULL)
X error("Cannot open %s", binfname);
X fprintf(convert,
X "Converting %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
X txtfname, info.type, info.auth);
X fwrite((char*)&info, sizeof(info), 1, binfile);
X
X nchars = stbuf.st_size;
X extra_chars = 127 - (nchars + 127) % 128;
X if (mode == TEXT)
X while (nchars--) {
X b = getc(txtfile);
X if (b == LF)
X b = CR;
X putc((char)b, binfile);
X }
X else
X while (nchars--)
X putc(getc(txtfile), binfile);
X
X while (extra_chars--)
X putc(0, binfile);
X fclose(binfile);
X fclose(txtfile);
X }
}
X
/* This procedure copies the input file to the output file, basically, although
X in TEXT mode it changes CR's to LF's and in any mode it skips over the Mac
X info header. */
X
re_other(mode)
X int mode;
{
X register ulong b;
X register ulong nchars;
X ulong temp;
X char txtfname[BINNAMELEN], binfname[BINNAMELEN];
X FILE *txtfile, *binfile;
X char *suffix;
X info_header info;
X
X if (mode == DATA)
X suffix = ".data";
X else if (mode == RSRC)
X suffix = ".rsrc";
X else
X suffix = ".text";
X
X while (hqxnames_left[0][0] != '-') {
X
X strcpy(binfname, *hqxnames_left++);
X if ((binfile = fopen(binfname, "r")) == NULL) {
X /*
X * Maybe we are supposed to figure out the suffix
X * ourselves?
X */
X strcat(binfname, ext);
X if (!(binfile = fopen(binfname, "r")))
X error("Cannot open %s", binfname);
X }
X /* Read the info from the .bin file, create the output file */
X fread((char*)&info, sizeof(info), 1, binfile);
X strncpy(txtfname, (char*)info.name, (int)info.nlen);
X txtfname[info.nlen] = '\0';
X fprintf(convert,
X "Converting %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
X txtfname, info.type, info.auth);
X
X /*
X * It appears that we use the unadorned name if possible,
X * and only add the suffix to avoid collisions.
X * This seems silly to me today (30Jul92), but
X * it looks deliberate, and we're avoiding gratuitous changes,
X * so we leave it as it is.
X */
X if ((txtfile = fopen(txtfname, "r")) == NULL) {
X if ((txtfile = fopen(txtfname, "w")) == NULL)
X error("Cannot open %s", txtfname);
X } else {
X fclose(txtfile);
X strcat(txtfname, suffix);
X if ((txtfile = fopen(txtfname, "w")) == NULL)
X error("Cannot open %s", txtfname);
X }
X
X bcopy((char*)info.dlen, (char *) &temp, 4);
X nchars = temp;
X if (mode == TEXT)
X while (nchars--) {
X b = getc(binfile);
X if (b == CR)
X b = LF;
X putc((char)b, txtfile);
X }
X else if (mode == DATA)
X while (nchars--)
X putc(getc(binfile), txtfile);
X else {
X while (nchars--)
X getc(binfile);
X bcopy((char*)info.rlen, (char *) &temp, 4);
X nchars = temp;
X while (nchars--)
X putc(getc(binfile), txtfile);
X }
X
X fclose(binfile);
X fclose(txtfile);
X }
}
X
usage()
{
X fprintf(stderr, Usage, cmdname);
X exit(1);
X /*NOTREACHED*/
}
X
SHAR_EOF
chmod 0444 mcvert.c ||
echo 'restore of mcvert.c failed'
Wc_c="`wc -c < 'mcvert.c'`"
test 19826 -eq "$Wc_c" ||
echo 'mcvert.c: original size 19826, current size' "$Wc_c"
fi
# ============= hqxify.c ==============
if test -f 'hqxify.c' -a X"$1" != X"-c"; then
echo 'x - skipping hqxify.c (File already exists)'
else
echo 'x - extracting hqxify.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'hqxify.c' &&
#include "mactypes.h"
X
/* HQXBUFLEN must be large enough to hold a complete hqx header */
#define HQXBUFLEN 512
byte hqxbuf[HQXBUFLEN + 1], *buf_ptr, *buf_end, *buf_start = hqxbuf + 1;
X
/* Note: line[0] stores the run length character,
X * so line is one greater than MAXLINE */
#define MAXLINE 2048
byte line[MAXLINE + 1], *line_ptr, *line_end, *line_start = line + 1;
X
/* keep the compiler happy */
#define LINE_START ((char*)(line_start))
X
extern FILE *convert;
extern FILE *debug;
extern FILE *verbose;
X
extern char *cmdname;
X
int line_count, file_count;
int save_state, total_bytes, save_run_length;
word save_accum;
char binfname[BINNAMELEN], hqxfname[BINNAMELEN];
FILE *hqxfile, *binfile;
X
/* This routine reads the header of a hqxed file and appropriately twiddles it,
X determines if it has CRC problems, creates the .bin file, and puts the info
X into the .bin file.
X Output is hqx_datalen, hqx_rsrclen, type, binfname, binfile */
X
hqx_to_bin_hdr(type, hqx_datalen, hqx_rsrclen)
X char *type;
X ulong *hqx_datalen, *hqx_rsrclen;
{
X register byte *hqx_ptr, *hqx_end;
X register ulong calc_crc;
X hqx_buf *hqx_block;
X hqx_header *hqx;
X info_header info;
X ulong mtim;
X short crc;
X
X extern word magic[];
X extern char *dir, *ext;
X extern short calc_mb_crc();
X
X /* read the hqx header, assuming that I won't exhaust hqxbuf in so doing */
X fill_hqxbuf();
X /*
X * If we are reading multiple files, then we could have the last
X * unread line of the "previous" file be just a colon (since we are
X * length driven, and stopped when we processed the expected
X * lengths), and the prior fill_hqxbuf() call would find it and
X * return 0, leaving the buffer unfilled. So, if we have zero
X * bytes so far, just fill it again.
X */
X if (total_bytes == 0) {
X fprintf(debug,
X "%s: Note: had to call fix_hqxbuf again in hqx_to_bin_hdr\n",
X cmdname);
X fill_hqxbuf();
X }
X if (total_bytes < MIN_HQX_HDR) {
X fprintf(verbose, "%s: %s (%d < %d): bad file format? -- exiting\n",
X cmdname, "error: hqx_header too short", total_bytes, MIN_HQX_HDR);
X exit(2);
X }
X
X hqx_block = (hqx_buf *) buf_ptr;
X hqx = (hqx_header *) (hqx_block->name + hqx_block->nlen);
X hqx_ptr = buf_ptr;
X hqx_end = (byte *) hqx + sizeof(hqx_header) - 1;
X calc_crc = 0;
X while (hqx_ptr < hqx_end)
X calc_crc = (((calc_crc & 0xff) << 8) | *hqx_ptr++) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X buf_ptr = hqx_ptr;
X
X /* stuff the hqx header data into the info header */
X bzero((char*)&info, sizeof(info_header));
X info.nlen = hqx_block->nlen;
X strncpy((char*)info.name, (char*)hqx_block->name, (int)info.nlen);/* name */
X bcopy((char*)hqx->type, (char*)info.type, 9); /* type, author, flag */
X info.flags &= 0x7e; /* reset lock bit, init bit */
X if (hqx->protect & 0x40)
X info.protect = 1; /* copy protect bit */
X bcopy((char*)hqx->dlen, (char*)info.dlen, 8); /* dlen, rlen */
X mtim = time2mac((ulong)time((long*)0));
X bcopy((char*)&mtim, (char*)info.mtim, 4);
X bcopy((char*)&mtim, (char*)info.ctim, 4);
X info.uploadvers = '\201';
X info.readvers = '\201';
X
X /* calculate MacBinary CRC */
X crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
X info.crc[0] = (char) (crc >> 8);
X info.crc[1] = (char) crc;
X
X /* Create the .bin file and write the info to it */
X unixify((char*)hqx_block->name);
X sprintf(binfname, "%s/%s%s", dir, hqx_block->name, ext);
X fprintf(convert,
X "Converting %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
X hqx_block->name, info.type, info.auth);
X
X if ((binfile = fopen(binfname, "w")) == NULL)
X error("Cannot open %s for write", binfname);
X check_hqx_crc((word)calc_crc, "File header CRC mismatch in %s", binfname);
X fwrite((char*)&info, sizeof(info), 1, binfile);
X
X /* Get a couple of items we'll need later */
X bcopy((char*)info.dlen, (char*)hqx_datalen, 4);
X *hqx_datalen = mac2long(*hqx_datalen);
X bcopy((char*)info.rlen, (char*)hqx_rsrclen, 4);
X *hqx_rsrclen = mac2long(*hqx_rsrclen);
X bcopy((char*)info.type, (char*)type, 4);
X
X /* emit useful debugging info */
X fprintf(debug, "\tdata_len=%10ld\t\ttrsrc_len=%10ld\n",
X *hqx_datalen, *hqx_rsrclen);
X
}
X
/* This routine reads the header of a bin file and appropriately twiddles it,
X creates the .hqx file, and puts the info into the .hqx file.
X Output is hqx_datalen, hqx_rsrclen, type, hqxfname, hqxfile */
X
bin_to_hqx_hdr(hqx_datalen, hqx_rsrclen)
X ulong *hqx_datalen, *hqx_rsrclen;
{
X register byte *hqx_ptr, *hqx_end;
X register ulong calc_crc;
X hqx_buf *hqx_block;
X hqx_header *hqx;
X info_header info;
X extern word magic[];
X extern char **hqxnames_left;
X extern char *ext;
X
X strcpy(binfname, *hqxnames_left++);
X if (!(binfile = fopen(binfname, "r"))) {
X /* Maybe we are supposed to figure out the suffix ourselves? */
X strcat(binfname, ext);
X if (!(binfile = fopen(binfname, "r")))
X error("Cannot open %s for read", binfname);
X }
X if (!fread((char*)&info, sizeof(info), 1, binfile))
X error("Unexpected EOF in header of %s", binfname);
X
X /* stuff the info header into the hqx header */
X hqx_block = (hqx_buf *) buf_ptr;
X hqx_block->nlen = info.nlen;
X strncpy((char*)hqx_block->name, (char*)info.name, (int)info.nlen);
X hqx = (hqx_header *) (hqx_block->name + hqx_block->nlen);
X hqx->version = 0;
X bcopy((char*)info.type, (char*)hqx->type, 9); /* type, author, flags */
X hqx->flags &= 0x7e; /* reset lock bit, init bit */
X if (info.protect = 1)
X hqx->protect = 0; /* protect bit: 0x40 */
X else
X hqx->protect = 0;
X bcopy((char*)info.dlen, (char*)hqx->dlen, 8); /* dlen, rlen */
X
X /* Create the .hqx file and write the info to it */
X strncpy(hqxfname, (char*)info.name, (int)info.nlen);
X hqxfname[info.nlen] = '\0';
X unixify(hqxfname);
X fprintf(convert,
X "Converting %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
X hqxfname, info.type, info.auth);
X
X calc_crc = 0;
X hqx_ptr = (byte *) hqx_block;
X hqx_end = hqx_ptr + hqx_block->nlen + sizeof(hqx_header);
X while (hqx_ptr < hqx_end)
X calc_crc = (((calc_crc & 0xff) << 8) | *hqx_ptr++) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X buf_ptr = hqx_end;
X write_hqx_crc((word)calc_crc);
X
X /* Get a couple of items we'll need later */
X bcopy((char*)info.dlen, (char*)hqx_datalen, 4);
X *hqx_datalen = mac2long(*hqx_datalen);
X bcopy((char*)info.rlen, (char*)hqx_rsrclen, 4);
X *hqx_rsrclen = mac2long(*hqx_rsrclen);
}
X
X
/* This routine copies bytes from the decoded input stream to the output.
X It also pads to a multiple of 128 bytes on the output, which is part
X of the .bin format */
word
hqx_to_bin_fork(nbytes)
X register ulong nbytes;
{
X register byte *cp;
X register ulong calc_crc;
X register int c_length;
X ulong extra_bytes;
X extern word magic[];
X long avail = 0; /* used for internal consistency checking */
X int wrote;
X
X extra_bytes = 127 - (nbytes + 127) % 128; /* pad fork to mult of
X * 128 bytes */
X calc_crc = 0;
X for (;;) {
X cp = buf_ptr;
X c_length = (cp + nbytes > buf_end) ? buf_end - cp : nbytes;
X /* we can only check readily if we read it here */
X if (avail && c_length > avail)
X error("hqx_to_bin_fork: writing %ld too many bytes",
X (char*)c_length - avail);
X nbytes -= c_length;
X wrote = fwrite((char*)cp, sizeof(byte), c_length, binfile);
X if (wrote != c_length)
X error("hqx_to_bin_fork: fwrite on binfile wrote %ld bytes too few",
X (char*)c_length-wrote);
X while (c_length--)
X calc_crc = (((calc_crc & 0xff) << 8) | *cp++) ^ magic[calc_crc >> 8];
X if (!nbytes)
X break;
X avail = fill_hqxbuf();
X }
X buf_ptr = cp;
X while (extra_bytes--)
X putc(0, binfile);
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X return (word) calc_crc;
}
X
/* This routine copies bytes from the input stream to the encoded output.
X It also pads to a multiple of 128 bytes on the input, which is part
X of the .bin format */
word
bin_to_hqx_fork(nbytes)
X register ulong nbytes;
{
X register byte *cp;
X register ulong calc_crc;
X register int c_length;
X ulong extra_bytes;
X extern word magic[];
X
X extra_bytes = 127 - (nbytes + 127) % 128; /* pad fork to mult of
X * 128 bytes */
X calc_crc = 0;
X for (;;) {
X cp = buf_ptr;
X c_length = (cp + nbytes > buf_end) ? buf_end - cp : nbytes;
X nbytes -= c_length;
X fread((char*)cp, sizeof(byte), c_length, binfile);
X buf_ptr += c_length;
X while (c_length--)
X calc_crc = (((calc_crc & 0xff) << 8) | *cp++) ^ magic[calc_crc >> 8];
X if (!nbytes)
X break;
X empty_hqxbuf();
X }
X buf_ptr = cp;
X
X fseek(binfile, (long)extra_bytes, 1);
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[calc_crc >> 8];
X return (word) calc_crc;
}
X
/* Essentials for Binhex 8to6 run length encoding */
#define RUNCHAR 0x90
#define MAXRUN 255
#define IS_LEGAL <0x40
#define ISNT_LEGAL >0x3f
#define DONE 0x7F /* tr68[':'] = DONE, since Binhex terminator is ':' */
#define SKIP 0x7E /* tr68['\n'|'\r'] = SKIP, i. e. end of line char. */
X /* We also treat '\0' as SKIP to handle files without trailing newline */
#define FAIL 0x7D /* character illegal in binhex file */
X
byte tr86[] =
"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
byte tr68[] = {
X SKIP, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
X 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
X 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
X 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
X 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
X 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
X 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
X 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
X 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
X 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
X 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
X FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
};
X
/*
X * This procedure transparently reads and decodes the hqx input.
X * It does run length and 6 to 8 decoding.
X * Global Input
X * buf_start
X * line_ptr
X * save_*
X * Global Output
X * buf_ptr end of buffer used
X * line_ptr
X * save_*
X * Internal
X * line":holds the encoded incoming ascii line.
X * buf: holds the decoding binary binary.
X * buf[-1] holds the character to be repeated for run length encoding.
X * As many lines as required are read to fill up buf.
X */
#define READING 0
#define SKIPPING 1
#define FIND_START_COLON 2
#define FINDING_COLON 3
X
/*
X * CAUTION: the control flow is quite confusing and full of special
X * cases; this routine should be rewritten. But if it ain't broke
X * (anymore), don't fix it . . .
X * NO! It's just too ugly to hack anymore! Next time, rewrite!
X * It's too bad we can't look for ``(This file ..'' when hunting for a
X * starting colon, but we don't emit such a line at the head of every
X * segment, so we can't expect to find one, now, can we?
X *
X * We were really too brittle when scanning, since we only
X * accepted lines which were exactly 64 (HQXLINELEN) characters long.
X * Fixed in version 1.83
X */
/* returns the number of bytes read */
fill_hqxbuf()
{
X register ulong c, accum;
X register int not_in_a_run = TRUE, state68;
X register byte *fast_buf, *fast_line;
X static int phase = FIND_START_COLON;
X long avail;
X
X buf_ptr = fast_buf = buf_start;
X fast_line = line_ptr;
X state68 = save_state;
X accum = save_accum;
X if (save_run_length > 0) {
X c = save_run_length;
X save_run_length = 0;
X goto continue_run;
X }
X
X while (fast_buf < buf_end) {
X
next_char:
X if ((c = *fast_line++) ISNT_LEGAL) {
X if (c == DONE) {
X /* done processing the file, so get out */
X /* phase has already been set by read processing */
X break;
X }
X
X next_line:
X if (!fgets(LINE_START, MAXLINE, hqxfile) && !new_in_hqx_file()) {
X /*
X * Used to assume if no more input available through error or
X * exhaustion while looking for valid data then must be done.
X * But we are demand driven (now) and if called, must
X * find data. So we don't silently exit any more.
X */
X error("Premature EOF while reading %s", hqxfname);
X }
X line_ptr = line_start;
X
X fprintf(debug, "DEBUG: input: %s\n", line_start);
X
X scan_line:
X
X /* ensure the the entire line is valid */
X
X fast_line = line_ptr;
X while ((*fast_line = tr68[*fast_line]) IS_LEGAL)
X fast_line++;
X
X /* grab the stopper */
X c = *fast_line;
X
X /* check for validity, transition phases as required */
X switch (phase) {
X
X case READING:
X case SKIPPING:
X case FINDING_COLON:
X if (c == SKIP && fast_line > line_ptr) {
X /* the entire line is valid (again), so (resume) reading */
X /* hack: we require the line to be non-empty to make the
X * existing logic work */
X phase = READING;
X break;
X }
X if (c == DONE && tr68[fast_line[1]] == SKIP) {
X /*
X * valid encoded last line, so
X * set phase for next line, and process this line --
X * we exit the fill-the-buffer loop when the terminal
X * colon is encountered during line processing
X */
X phase = FIND_START_COLON;
X break;
X }
X
X /* line is not entirely valid, so do some flavor of skipping */
X phase = (phase == FINDING_COLON) ? FIND_START_COLON : SKIPPING;
X goto next_line;
X
X case FIND_START_COLON:
X if (*line_start == DONE) {
X /* can't transition to READING
X * until know that entire line is valid
X * so transition to intermediate state
X */
X phase = FINDING_COLON;
X /* skip the initial colon */
X line_ptr++;
X goto scan_line;
X }
X goto next_line;
X
X }
X
X /* we've read in a valid line, so start processing it */
X fast_line = line_ptr;
X c = *fast_line++;
X
X /*
X * Jskud 15Jul92: fix bug reported by Info-Mac Moderator Bill
X * regarding case of last line just :
X * The line is valid, but it has no data, so don't ingest it.
X */
X
X if (c == DONE)
X break;
X
X fprintf(debug, "DEBUG: processing line\n");
X
X }
X
X /* Finally, we have the next 6 bits worth of data in "c" as input. */
X /* Note: we use "c" as the output of this processing too */
X switch (state68++) {
X case 0:
X accum = c;
X goto next_char;
X case 1:
X accum = (accum << 6) | c;
X c = accum >> 4;
X break;
X case 2:
X accum = (accum << 6) | c;
X c = (accum >> 2) & 0xff;
X break;
X case 3:
X /* we avoid any miniscule optimizations here
X * to maintain parallelism and clarity
X * which should enhance program maintainability
X */
X accum = (accum << 6) | c;
X c = accum & 0xff;
X state68 = 0;
X break;
X }
X if (not_in_a_run)
X if (c != RUNCHAR)
X *fast_buf++ = c;
X else {
X not_in_a_run = FALSE;
X goto next_char;
X }
X else {
X /* "c" has the run total length, not just the incremental,
X hence the post decrement is correct */
X if (c--) {
X avail = buf_end - fast_buf;
X if (c > avail) {
X save_run_length = c - avail;
X c = avail;
X }
X continue_run:
X {
X register char ch = fast_buf[-1];
X
X while (c--)
X *fast_buf++ = ch;
X }
X
X } else
X /* handle special case of 0x9000 => 0x90 */
X *fast_buf++ = RUNCHAR;
X
X not_in_a_run = TRUE;
X }
X }
X
X avail = fast_buf - buf_ptr;
X total_bytes += avail;
X buf_start[-1] = fast_buf[-1];
X line_ptr = fast_line;
X save_state = state68;
X save_accum = accum;
X
X return avail;
X
}
X
X
new_in_hqx_file()
{
X char *hqx_ext;
X extern char **hqxnames_left;
X int result;
X
X fprintf(debug, "entering new_in_hqx_file ...\n");
X
X if (*hqxnames_left[0] == '\0' || *hqxnames_left[0] == '-') {
X result = FALSE;
X goto exit;
X }
X
X strcpy(hqxfname, *hqxnames_left++);
X hqx_ext = hqxfname + strlen(hqxfname) - 4;
X if (!strcmp(hqx_ext, ".hqx")) {
X if (!freopen(hqxfname, "r", hqxfile))
X error("Cannot open %s for read", hqxfname);
X } else {
X if (!freopen(hqxfname, "r", hqxfile)) {
X hqx_ext += 4;
X strcpy(hqx_ext, ".hqx");
X if (!freopen(hqxfname, "r", hqxfile)) {
X error("Cannot open %s for read", hqxfname);
X }
X }
X }
X fgets(LINE_START, MAXLINE, hqxfile);
X result = TRUE;
X
exit:
X if (result == TRUE)
X fprintf(debug, "... opened %s\n", hqxfname);
X else
X fprintf(debug, "... nothing to open\n");
X
X return result;
}
X
/*
X * This procedure transparently encodes and writes the hqx output.
X * It does run length and 8 to 6 encoding.
X */
empty_hqxbuf()
{
X register ulong c, accum, last_c;
X register byte *fast_buf, *fast_line;
X register int state86, dont_look_for_runs = FALSE, run_length;
X extern int maxlines;
X
X run_length = save_run_length;
X last_c = buf_start[-1];
X fast_buf = buf_start;
X fast_line = line_ptr;
X state86 = save_state;
X accum = save_accum;
X while (fast_buf < buf_ptr) {
X c = *fast_buf++;
X if (dont_look_for_runs)
X dont_look_for_runs = FALSE;
X else if (last_c == c && run_length < MAXRUN) {
X run_length++;
X continue;
X } else {
X if (run_length > 1) {
X --fast_buf;
X if (run_length == 2 && last_c != RUNCHAR)
X c = last_c;
X else {
X c = RUNCHAR;
X *--fast_buf = run_length;
X dont_look_for_runs = TRUE;
X }
X run_length = 1;
X } else
X last_c = c;
X if (c == RUNCHAR && !dont_look_for_runs) {
X *--fast_buf = 0;
X dont_look_for_runs = TRUE;
X }
X }
X
X if (fast_line == line_end) {
X if (line_count++ == maxlines)
X new_out_hqx_file();
X fputs(LINE_START, hqxfile);
X fast_line = line_start;
X }
X switch (state86++) {
X case 0:
X *fast_line++ = tr86[c >> 2];
X accum = (c << 4) & 0x3f;
X break;
X case 1:
X *fast_line++ = tr86[(c >> 4) | accum];
X accum = (c << 2) & 0x3f;
X break;
X case 2:
X *fast_line++ = tr86[(c >> 6) | accum];
X if (fast_line == line_end) {
X if (line_count++ == maxlines)
X new_out_hqx_file();
X fputs(LINE_START, hqxfile);
X fast_line = line_start;
X }
X *fast_line++ = tr86[c & 0x3f];
X state86 = 0;
X break;
X }
X }
X save_run_length = run_length;
X buf_start[-1] = last_c;
X buf_ptr = buf_start;
X line_ptr = fast_line;
X save_state = state86;
X save_accum = accum;
}
X
new_out_hqx_file()
{
X char filename[NAMELEN + 7];
X extern int maxlines;
X int rc;
X
X fprintf(hqxfile, "<<< End of Part %2d >>>\n", file_count);
X rc = fclose(hqxfile);
X if (rc == EOF)
X error("Error closing hqxfile", (char*)0);
X file_count++;
X if (maxlines)
X sprintf(filename, "%s%02d.hqx", hqxfname, file_count);
X else
X sprintf(filename, "%s.hqx", hqxfname);
X if ((hqxfile = fopen(filename, "w")) == NULL)
X error("Cannot open %s for write", filename);
X if (file_count > 1)
X fprintf(hqxfile, "<<< Start of Part %2d >>>\n", file_count);
X else
X fprintf(hqxfile, "(This file must be converted with BinHex 4.0)\n\n");
X line_count = 3;
}
X
check_hqx_crc(calc_crc, msg, name)
X word calc_crc;
X char msg[], name[];
X
{
X word read_crc;
X
X if (buf_ptr >= buf_end)
X fill_hqxbuf();
X read_crc = *buf_ptr++ << 8;
X if (buf_ptr >= buf_end)
X fill_hqxbuf();
X read_crc |= *buf_ptr++;
X if (read_crc != calc_crc)
X error(msg, name);
}
X
write_hqx_crc(calc_crc)
X word calc_crc;
{
X if (buf_ptr == buf_end)
X empty_hqxbuf();
X *buf_ptr++ = calc_crc >> 8;
X if (buf_ptr == buf_end)
X empty_hqxbuf();
X *buf_ptr++ = calc_crc;
}
X
un_hqx(unpit_flag)
X int unpit_flag;
{
X char type[4];
X ulong hqx_datalen, hqx_rsrclen;
X word un_pit();
X int unpitting, bytes_read;
X word calc_crc;
X extern char **hqxnames_left;
X
X hqxfile = fopen("/dev/null", "r");
X if (hqxfile == NULL)
X error("Cannot open %s for read", "/dev/null");
X line_end = line_start + HQXLINELEN;
X buf_end = buf_start + HQXBUFLEN;
X
X while (*hqxnames_left[0] != '\0' && *hqxnames_left[0] != '-') {
X /* that is, while we still have files to process */
X total_bytes = 0;
X line_ptr = line_start;
X line_ptr[0] = SKIP;
X save_state = 0;
X save_run_length = 0;
X
X hqx_to_bin_hdr(type, &hqx_datalen, &hqx_rsrclen); /* binfname */
X
X unpitting = unpit_flag && !strcmp(type, "PIT ");
X if (unpitting) {
X fclose(binfile);
X unlink(binfname);
X bytes_read = total_bytes - (buf_end - buf_ptr);
X calc_crc = un_pit();
X bytes_read = total_bytes - (buf_end - buf_ptr) - bytes_read;
X if (bytes_read != hqx_datalen)
X fprintf(verbose,
X "Warning - Extraneous characters ignored in %s\n", binfname);
X } else {
X calc_crc = hqx_to_bin_fork(hqx_datalen);
X }
X check_hqx_crc(calc_crc, "File data CRC mismatch in %s", binfname);
X
X calc_crc = hqx_to_bin_fork(hqx_rsrclen);
X check_hqx_crc(calc_crc, "File rsrc CRC mismatch in %s", binfname);
X
X if (!unpitting)
X fclose(binfile);
X }
}
X
re_hqx()
{
X word calc_crc;
X ulong hqx_datalen, hqx_rsrclen;
X extern char **hqxnames_left;
X extern int maxlines;
X int rc;
X
X line_end = line_start + HQXLINELEN;
X buf_end = buf_start + HQXBUFLEN;
X while (*hqxnames_left[0] != '-') {
X hqxfile = fopen("/dev/null", "w");
X if (hqxfile == NULL)
X error("Cannot open %s for write", "/dev/null");
X
X /*
X * We use the trick of setting our line_count at the limit to
X * force an immediate transition on overflow.
X */
X line_count = maxlines;
X
X file_count = 0;
X line_ptr = line_start;
X *line_ptr++ = ':';
X strcpy((char*)line_end, "\n");
X buf_ptr = buf_start;
X save_state = 0;
X save_run_length = 1;
X
X bin_to_hqx_hdr(&hqx_datalen, &hqx_rsrclen); /* calculates hqxfname */
X
X /*
X * Now that we have hqxfname, start the new file if
X * not yet started. We no longer wait until the output
X * buffer overflows, since empty files with short names didn't overflow!
X */
X
X if (file_count == 0)
X new_out_hqx_file();
X
X calc_crc = bin_to_hqx_fork(hqx_datalen);
X write_hqx_crc(calc_crc);
X
X calc_crc = bin_to_hqx_fork(hqx_rsrclen);
X write_hqx_crc(calc_crc);
X /*
X * To end a run and to get the last stray bits,
X * temporarily add a char.
X */
X *buf_ptr = !buf_ptr[-1];
X buf_ptr++;
X empty_hqxbuf();
X /* now toss any extra output character generated */
X if (save_state != 2)
X --line_ptr;
X
X /* if we're at the end of the line now, write it out */
X if (line_ptr == line_end) {
X fputs(LINE_START, hqxfile);
X line_ptr = line_start;
X }
X
X /* paste the trailing colon onto the end of the line */
X /* recall that line_ptr points into the line, not at the line */
X strcpy((char*)line_ptr, ":\n");
X
X /* and flush the output buffer */
X fputs(LINE_START, hqxfile);
X
X rc = fclose(hqxfile);
X if (rc == EOF)
X error("Error closing hqxfile", (char*)0);
X }
}
SHAR_EOF
chmod 0444 hqxify.c ||
echo 'restore of hqxify.c failed'
Wc_c="`wc -c < 'hqxify.c'`"
test 23244 -eq "$Wc_c" ||
echo 'hqxify.c: original size 23244, current size' "$Wc_c"
fi
# ============= unpack.c ==============
if test -f 'unpack.c' -a X"$1" != X"-c"; then
echo 'x - skipping unpack.c (File already exists)'
else
echo 'x - extracting unpack.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'unpack.c' &&
#include "mactypes.h"
X
extern word magic[];
extern FILE *output;
extern char *dir, *ext;
X
ulong pit_datalen, pit_rsrclen;
word hqx_crc, write_pit_fork();
char pitfname[BINNAMELEN]; /* name of file being unpacked */
FILE *pitfile; /* output file */
X
branch branchlist[255], *branchptr, *read_tree();
leaf leaflist[256], *leafptr;
word Huff_nibble, Huff_bit_count;
X
byte(*read_char) (), get_crc_byte(), getHuffbyte();
X
word
un_pit()
{
X char PitId[4];
X int i;
X word pit_crc;
X
X hqx_crc = 0;
X /* Read and unpack until the PackIt End message is read */
X for (;;) {
X read_char = get_crc_byte;
X for (i = 0; i < 4; i++)
X PitId[i] = (char) get_crc_byte();
X if (!strncmp(PitId, "PEnd", 4))
X break;
X
X if (strncmp(PitId, "PMag", 4) && strncmp(PitId, "PMa4", 4))
X error("Unrecognized Packit format message %s", PitId);
X
X if (PitId[3] == '4') { /* if this file is compressed */
X branchptr = branchlist;/* read the Huffman decoding */
X leafptr = leaflist; /* tree that is on the input */
X Huff_bit_count = 0; /* and use Huffman decoding */
X read_tree(); /* subsequently */
X read_char = getHuffbyte;
X }
X read_pit_hdr(); /* also calculates datalen,
X * rsrclen, pitfile, pitfname */
X pit_crc = write_pit_fork(pit_datalen, (ulong)0);
X pit_crc = write_pit_fork(pit_rsrclen, (ulong)pit_crc);
X check_pit_crc(pit_crc, " File data/rsrc CRC mismatch in %s", pitfname);
X fclose(pitfile);
X }
X hqx_crc = (hqx_crc << 8) ^ magic[hqx_crc >> 8];
X hqx_crc = (hqx_crc << 8) ^ magic[hqx_crc >> 8];
X return hqx_crc;
}
X
check_pit_crc(calc_crc, msg, name)
X word calc_crc;
X char msg[], name[];
X
{
X word read_crc;
X
X read_crc = (*read_char) () << 8;
X read_crc |= (*read_char) ();
X if (read_crc != calc_crc)
X error(msg, name);
}
X
/* This routine reads the header of a packed file and appropriately twiddles it,
X determines if it has CRC problems, creates the .bin file, and puts the info
X into the .bin file.
X Output is pit_datalen, pit_rsrclen, pitfname, pitfile */
read_pit_hdr()
{
X register int n;
X register byte *pit_byte;
X register ulong pit_crc;
X pit_header pit;
X info_header info;
X short crc;
X
X extern short calc_mb_crc();
X
X /* read the pit header and compute the CRC */
X pit_crc = 0;
X pit_byte = (byte *) & pit;
X for (n = 0; n < sizeof(pit_header); n++) {
X *pit_byte = (*read_char) ();
X pit_crc = ((pit_crc & 0xff) << 8)
X ^ magic[*pit_byte++ ^ (pit_crc >> 8)];
X }
X
X /* stuff the pit header data into the info header */
X bzero((char*)&info, sizeof(info_header));
X info.nlen = pit.nlen;
X strncpy((char*)info.name, (char*)pit.name, (int)pit.nlen);/* name */
X bcopy((char*)pit.type, (char*)info.type, 9); /* type, author, flag */
X bcopy((char*)pit.dlen, (char*)info.dlen, 16); /* (d,r)len, (c,m)tim */
X info.flags &= 0x7e; /* reset lock bit, init bit */
X if (pit.protect & 0x40)
X info.protect = 1; /* copy protect bit */
X info.uploadvers = '\201';
X info.readvers = '\201';
X
X /* calculate MacBinary CRC */
X crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
X info.crc[0] = (char) (crc >> 8);
X info.crc[1] = (char) crc;
X
X /* Create the .bin file and write the info to it */
X pit.name[pit.nlen] = '\0';
X unixify((char*)pit.name);
X sprintf(pitfname, "%s/%s%s", dir, pit.name, ext);
X fprintf(output,
X " %-14s%-30s type = \"%4.4s\", author = \"%4.4s\"\n",
X (read_char == get_crc_byte) ? "Unpacking" : "Decompressing",
X pit.name, pit.type, pit.auth);
X if ((pitfile = fopen(pitfname, "w")) == NULL)
X error(" Cannot open %s", pitfname);
X check_pit_crc((word)pit_crc, " File header CRC mismatch in %s", pitfname);
X fwrite((char*)&info, sizeof(info_header), 1, pitfile);
X
X /* Get a couple of items we'll need later */
X bcopy((char*)pit.dlen, (char*)&pit_datalen, 4);
X pit_datalen = mac2long(pit_datalen);
X bcopy((char*)pit.rlen, (char*)&pit_rsrclen, 4);
X pit_rsrclen = mac2long(pit_rsrclen);
}
X
/* This routine copies bytes from the decoded input stream to the output
X and calculates the CRC. It also pads to a multiple of 128 bytes on the
X output, which is part of the .bin format */
word
write_pit_fork(nbytes, calc_crc)
X register ulong nbytes;
X register ulong calc_crc;
{
X register ulong b;
X int extra_bytes;
X
X extra_bytes = 127 - (nbytes + 127) % 128; /* pad fork to mult of
X * 128 bytes */
X while (nbytes--) {
X b = (*read_char) ();
X calc_crc = ((calc_crc & 0xff) << 8) ^ magic[b ^ (calc_crc >> 8)];
X putc((char)b, pitfile);
X }
X while (extra_bytes--)
X putc(0, pitfile);
X return (word) calc_crc;
}
X
/* This routine recursively reads the compression decoding data.
X It appears to be Huffman compression. Every leaf is represented
X by a 1 bit, then the byte it represents. A branch is represented
X by a 0 bit, then its zero and one sons */
branch *
read_tree()
{
X register branch *branchp;
X register leaf *leafp;
X register ulong b;
X
X if (!Huff_bit_count--) {
X Huff_nibble = get_crc_byte();
X Huff_bit_count = 7;
X }
X if ((Huff_nibble <<= 1) & 0x0100) {
X leafp = leafptr++;
X leafp->flag = 1;
X b = get_crc_byte();
X leafp->data = Huff_nibble | (b >> Huff_bit_count);
X Huff_nibble = b << (8 - Huff_bit_count);
X return (branch *) leafp;
X } else {
X branchp = branchptr++;
X branchp->flag = 0;
X branchp->zero = read_tree();
X branchp->one = read_tree();
X return branchp;
X }
}
X
/* This routine returns the next 8 bits. It finds the byte in the
X Huffman decoding tree based on the bits from the input stream. */
byte
getHuffbyte()
{
X register branch *branchp;
X
X branchp = branchlist;
X while (!branchp->flag) {
X if (!Huff_bit_count--) {
X Huff_nibble = get_crc_byte();
X Huff_bit_count = 7;
X }
X branchp = ((Huff_nibble <<= 1) & 0x0100) ? branchp->one : branchp->zero;
X }
X return ((leaf *) branchp)->data;
}
X
/* This routine returns the next byte on the .hqx input stream, hiding
X most file system details at a lower level. .hqx CRC is maintained
X here */
byte
get_crc_byte()
{
X register ulong c;
X extern byte *buf_ptr, *buf_end;
X
X if (buf_ptr == buf_end)
X fill_hqxbuf();
X c = *buf_ptr++;
X hqx_crc = ((hqx_crc << 8) | c) ^ magic[hqx_crc >> 8];
X return (byte) c;
}
SHAR_EOF
chmod 0444 unpack.c ||
echo 'restore of unpack.c failed'
Wc_c="`wc -c < 'unpack.c'`"
test 6096 -eq "$Wc_c" ||
echo 'unpack.c: original size 6096, current size' "$Wc_c"
fi
# ============= mactypes.h ==============
if test -f 'mactypes.h' -a X"$1" != X"-c"; then
echo 'x - skipping mactypes.h (File already exists)'
else
echo 'x - extracting mactypes.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mactypes.h' &&
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
X
#ifdef TIMEVAL
# include <sys/param.h>
# include <sys/time.h>
#else
# include <sys/timeb.h>
#endif
X
/* Useful, though not particularly Mac related, values */
typedef unsigned char byte; /* one byte, obviously */
typedef unsigned short word; /* must be 2 bytes */
X
#ifndef ULONG
X typedef unsigned long ulong; /* 4 bytes */
#endif
X
/* declarations to keep lint informed */
char *calloc();
#include <string.h>
void exit();
long time();
ulong time2mac();
X
#define TRUE 1
#define FALSE 0
#define CR 0x0d
#define LF 0x0a
X
/* Compatibility issues */
#ifdef BSD
#define mac2word (word) ntohs
#define mac2long (ulong) ntohl
#define word2mac (word) htons
#define long2mac (ulong) htonl
#else
#define mac2word
#define mac2long
#define word2mac
#define long2mac
#endif
X
#ifdef MAXNAMLEN /* 4.2 BSD, stdio.h */
#define SYSNAMELEN MAXNAMLEN
#else
#define SYSNAMELEN DIRSIZ
#endif
X
#define NAMELEN 63 /* maximum legal Mac file name length */
#define BINNAMELEN 68 /* NAMELEN + len(".bin\0") */
X
/* Format of a bin file:
A bin file is composed of 128 byte blocks. The first block is the
info_header (see below). Then comes the data fork, null padded to fill the
last block. Then comes the resource fork, padded to fill the last block. A
proposal to follow with the text of the Get Info box has not been implemented,
to the best of my knowledge. Version, zero1 and zero2 are what the receiving
program looks at to determine if a MacBinary transfer is being initiated.
*/
typedef struct { /* info file header (128 bytes). Unfortunately,
X * these longs don't align to word boundaries */
X
/* decimal offset */
X
/* 000 */
X byte version; /* there is only a version 0 at this time */
/* 001 */
X byte nlen; /* Length of filename. */
/* 002 */
X byte name[NAMELEN]; /* Filename (only 1st nlen are significant) */
/* 065 */
X byte type[4]; /* File type. */
/* 069 */
X byte auth[4]; /* File creator. */
/* 073 */
X byte flags; /* file flags: LkIvBnSyBzByChIt */
X /* Locked, Invisible,Bundle, System */
X /* Bozo, Busy, Changed, Init */
/* 074 */
X byte zero1;
/* 075 */
X byte icon_vert[2]; /* Vertical icon position within window */
/* 077 */
X byte icon_horiz[2]; /* Horizontal icon postion in window */
/* 079 */
X byte window_id[2]; /* Window or folder ID. */
/* 081 */
X byte protect; /* = 1 for protected file, 0 otherwise */
/* 082 */
X byte zero2;
/* 083 */
X byte dlen[4]; /* Data Fork length (bytes) - most sig. */
/* 087 */
X byte rlen[4]; /* Resource Fork length byte first */
/* 091 */
X byte ctim[4]; /* File's creation date. */
/* 095 */
X byte mtim[4]; /* File's "last modified" date. */
/* 099 */
X byte ilen[2]; /* GetInfo message length */
/* 101 */
X byte flags2; /* Finder flags, bits 0-7 */
/* 102 */
X byte unused[14];
/* 116 */
X byte packlen[4]; /* length of total files when unpacked */
/* 120 */
X byte headlen[2]; /* length of secondary header */
/* 122 */
X byte uploadvers; /* Version of MacBinary II that the uploading
X * program is written for */
/* 123 */
X byte readvers; /* Minimum MacBinary II version needed to read
X * this file */
/* 124 */
X byte crc[2]; /* CRC of the previous 124 bytes */
/* 126 */
X byte padding[2]; /* two trailing unused bytes */
/* 128 */
} info_header;
X
/* The *.info file of a MacTerminal file transfer either has exactly this
structure or has the protect bit in bit 6 (near the sign bit) of byte zero1.
The code I have for macbin suggests the difference, but I'm not so sure */
X
/*
X * Format of a hqx file:
X * (but see also binhex-40-specs.txt for legal variations)
X *
X * It begins with a line that begins "(This file" and the rest is 64
X * character lines (except possibly the last, and not including
X * newlines) where the first begins and the last ends with a colon.
X * The characters between colons should be only from the set in tr86,
X * below, each of which corresponds to 6 bits of data. Once that is
X * translated to 8 bit bytes, you have the real data, except that the
X * byte 0x90 may indicate, if the following character is nonzero, that
X * the previous byte is to be repeated 1 to 255 times all together
X * (that is, the count following 0x90 contains the total run length,
X * not the marginal repeat count). The byte 0x90 is represented by
X * 0x9000. The information in the file is the hqx_buf (see below), a
X * CRC word, the data fork, a CRC word, the resource fork, and a CRC
X * word. There is considerable confusion about the flags. An
X * official looking document unclearly states that the init bit is
X * always clear, as is the following byte. The experience of others
X * suggests, however, that this is not the case.
X */
X
/* NOTE:
X * Jskud 25Jun92
X
X * a hqx file has no date/time information, and a binary constructed
X * from a hqx file will use the current time; therefore, reconverting
X * a hqx file repeatedly will generate different bin files!
X
X * The "flags" byte can change when refetched, for example, whether or
X * not the init bit is set (that is, the Finder has "seen" the file);
X * this can affect the hqx file, including the run length encoding, so
X * the hqx files can look quite different, although the actual
X * difference is miniscule; also, the init bit difference will not be
X * present when the hqx file is converted to a bin file, since both the
X * locked and init bits are cleared on bin file creation.
X
X * Because we've experienced this "spurious" difference, and since
X * the init bit is only thought meaningful when running on the Mac,
X * and to establish symmetry between bin file creation and hqx file
X * creation, we unconditionally clear the init and locked bits when
X * creating the hqx file.
X
X */
X
#define HQXLINELEN 64
typedef struct {
/* decimal offset */
/* 000 */
X byte version; /* there is only a version 0 at this time */
/* 001 */
X byte type[4]; /* File type. */
/* 005 */
X byte auth[4]; /* File creator. */
/* 009 */
X byte flags; /* file flags: LkIvBnSyBzByChIt */
/* 010 */
X byte protect; /* ?Pr??????, don't know what ? bits mean */
/* 011 */
X byte dlen[4]; /* Data Fork length (bytes) - most sig. */
/* 015 */
X byte rlen[4]; /* Resource Fork length byte first */
/* 019 */
X byte bugblank; /* to fix obscure sun 3/60 problem that always
X * makes sizeof(hqx_header) even */
/* 020 */
} hqx_header;
X
typedef struct { /* hqx file header buffer (includes file name) */
X byte nlen; /* Length of filename. */
X byte name[NAMELEN]; /* Filename: only nlen actually appear */
X hqx_header all_the_rest;/* and all the rest follows immediately */
} hqx_buf;
X
/* every valid hqx header is at least this long */
#define MIN_HQX_HDR (sizeof(hqx_header) + 2)
X
/*
X * The minimum number of hqx_lines in a file to ensure that we have
X * enough lines to emit the entire header before starting a new file.
X * + 1 is for rounding, + 2 is for "(This file " and the blank line.
X */
#define MIN_HQX_LINES (sizeof(hqx_buf) * 4 / 3 / HQXLINELEN + 1 + 2)
X
X
/* Format of a Packit file:
Repeat the following sequence for each file in the Packit file:
X 4 byte identifier ("PMag" = not compressed, "Pma4" = compressed)
X 320 byte compression data (if compressed file)
X = preorder transversal of Huffman tree
X 255 0 bits corresponding to nonleaf nodes
X 256 1 bits corresponding to leaf nodes
X 256 bytes associating leaf nodes with bytes
X 1 completely wasted bit
X 92 byte header (see pit_header below) *
X 2 bytes CRC word for header *
X data fork (length from header) *
X resource fork (length from header) *
X 2 bytes CRC word for forks *
X
Last file is followed by the 4 byte Ascii string, "Pend", and then the EOF.
The CRC calculations differ from those in the binhex format.
X
* these are in compressed form if compression is on for the file
X
*/
X
typedef struct { /* Packit file header (92 bytes) */
X byte nlen; /* Length of filename. */
X byte name[NAMELEN]; /* Filename (only 1st nlen are significant) */
X byte type[4]; /* File type. */
X byte auth[4]; /* File creator. */
X byte flags; /* file flags: LkIvBnSyBzByChIt */
X byte zero1;
X byte protect; /* = 1 for protected file, 0 otherwise */
X byte zero2;
X byte dlen[4]; /* Data Fork length (bytes) - most sig. */
X byte rlen[4]; /* Resource Fork length byte first */
X byte ctim[4]; /* File's creation date. */
X byte mtim[4]; /* File's "last modified" date. */
} pit_header;
X
/* types for constructing the Huffman tree */
typedef struct branch_st {
X byte flag;
X struct branch_st *one, *zero;
} branch;
X
typedef struct leaf_st {
X byte flag;
X byte data;
} leaf;
SHAR_EOF
chmod 0444 mactypes.h ||
echo 'restore of mactypes.h failed'
Wc_c="`wc -c < 'mactypes.h'`"
test 9603 -eq "$Wc_c" ||
echo 'mactypes.h: original size 9603, current size' "$Wc_c"
fi
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
CSOURCES = mcvert.c hqxify.c unpack.c
HSOURCES = mactypes.h
SOURCES = $(CSOURCES) $(HSOURCES) Makefile
sharSOURCES = $(SOURCES) mcvert.1
OBJECTS = mcvert.o hqxify.o unpack.o
ALL = mcvert mcvert.man
CLEAN = $(OBJECTS) $(ALL)
X
VERSION=183
X
BIN = .
X
## Last Defintion Wins ##
X
## Machine CFLAGS
CFLAGS_M= -DTIMEVAL # for machines without <timeb.h> and ftime()
CFLAGS_M= -DULONG # for machines with ulong pre-defined (eg, SGI)
CFLAGS_M= -DTIMEVAL -DULONG # eg, for A/UX
CFLAGS_M= # for many machines (eg, HP-UX, SunOS)
X
## Debugging/optimization CFLAGS
CFLAGS_D = -g # debugging
CFLAGS_D = -O # optimization
X
## All CFLAGS
CFLAGS=$(CFLAGS_M) $(CFLAGS_D)
X
all: $(ALL)
X
mcvert: $(OBJECTS)
X $(CC) $(CFLAGS) $(OBJECTS) -o $(BIN)/mcvert
X
lint: $(HSOURCES) $(CSOURCES)
X lint $(CSOURCES)
X
$(OBJECTS): mactypes.h Makefile
X
print: $(SOURCES)
X lpr -p -P$(PRINTER) $(SOURCES)
X
shar : mcvert-$(VERSION).shar
mcvert-$(VERSION).shar : $(sharSOURCES)
X shar $(sharSOURCES) > $@
X
clean:
X rm -f $(CLEAN)
X
mcvert.man: mcvert.1
X nroff -man mcvert.1 | sed 's/.//g' > $@
mcvert.ps: mcvert.1
X groff -man mcvert.1 > $@
SHAR_EOF
chmod 0444 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 1108 -eq "$Wc_c" ||
echo 'Makefile: original size 1108, current size' "$Wc_c"
fi
# ============= mcvert.1 ==============
if test -f 'mcvert.1' -a X"$1" != X"-c"; then
echo 'x - skipping mcvert.1 (File already exists)'
else
echo 'x - extracting mcvert.1 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mcvert.1' &&
.TH MCVERT LOCAL "03Aug92"
.UC 4.2
.SH NAME
mcvert \- BinHex 4.0 to MacBinary file conversion utility
.SH SYNOPSIS
.B mcvert
{ [options] name ... } ...
.br
.SH DESCRIPTION
The
.I mcvert
program translates files between MacBinary format and
other formats often used in exchanging Macintosh files.
See
.I FILE FORMATS
below for a description of the file formats supported.
.SH PARAMETERS
The defaults for the parameters are
.RB - xDqv :
convert BinHex4.0 files
.RB ( x )
to MacBinary files
.RB ( D ),
bypass automatic unpacking of PIT files
.RB ( q ),
and provide a verbose level of output
.RB ( v ).
.SH "OPTIONS"
All the options, other than
.I "FORMAT OPTIONS"
described below, are listed here.
XFrom each set, one and only one alternative is active for any one file.
.TP
.B U | D
When option 'U' is selected, the conversion that takes place is the one suitable
for Uploading files. That is, the conversion is from MacBinary to something
else when 'U' is selected. Conversely, option 'D', as in Download,
converts from something to MacBinary.
.TP
.B p | q
If a BinHex to MacBinary conversion is taking place and option 'p' is selected,
any file of type "PIT "
will be unpacked into its constituent parts. This option does not recursively
unpack "PIT " files packed in "PIT " files.
If a MacBinary to BinHex conversion is taking place, this option is currently
ignored.
.TP
.B
S | s | V | v
Normally,
.I mcvert
prints to stderr information about the files it is creating.
Selecting option 'S', as in Silent, disables all such reporting.
Option 's', as in silent, disables all but the "Converting ..." messages.
Option 'V', for very Verbose, displays debugging information as well.
Option 'v', as in verbose, emits generally useful information.
.SH "FILE FORMATS"
The primary formats in which Macintosh files are represented on non-Macs are:
.TP
.B MacBinary:
An eight bit wide representation of the data and resource forks of a Mac
file and of relevant Finder information, MacBinary files are recognized
as "special" by several Macintosh terminal emulators. These emulators,
using Kermit or Xmodem or any other file transfer protocol, can separate
the incoming file into forks and appropriately modify the Desktop to display
icons, types, creation dates, and the like.
.TP
.B BinHex 4.0:
A seven bit wide representation of a Mac file with CRC error checking,
BinHex 4.0 files are designed for communication of Mac files over long
distance, possibly noisy, seven bit wide paths.
.TP
.B PackIt:
PackIt files are actually representations of collections of Mac files, possibly
Huffman compressed. Packing many small related files together before
a MacBinary transfer or a translation to BinHex 4.0 is common practice.
.TP
.B Text:
A Macintosh ends each line of a plain text file with a carriage return
character (^M), rather than the newline character (^J) that some systems
seem to prefer. Moreover, a MacBinary file has prepended Finder information
that non-Macintoshes don't need.
.TP
.B Data, Rsrc:
A Data or Rsrc file is the exact copy of the data or resource fork of a
Macintosh file.
.SH "FORMAT OPTIONS"
Exactly one of the following selections may be specified for an input name:
.TP
.B x
BinHex 4.0 - files in the MacBinary format are translated to BinHex
files, or vice versa. The name argument may be the name of a file to be
converted or a basename to which an appropriate suffix must be appended
to get a filename. If the conversion is from Binhex 4.0 to MacBinary,
several files may comprise the BinHex representation of the Mac file.
Rather than manually concatenate the files and manually delete mail
headers and other extraneous garbage, one may specify the names of the
files in order and
.I mcvert
will do the concatenating and deleting. Conversely, in converting
a MacBinary file to BinHex 4.0 format for mailing over long distances,
one may be restricted to mail messages of no greater that some fixed
length. In this case,
.I mcvert
can automatically divide the BinHex file into pieces and label each
piece appropriately.
For details on automatically segmenting files, see the description of the
.B MAC_LINE_LIMIT
environment variable below.
.TP
.B r
Resource - files in the MacBinary format with empty data forks
and nonempty resource forks are made from ordinary data files, or vice versa.
.TP
.B d
Data - files in the MacBinary format with nonempty data forks
and empty resource forks are made from ordinary data files, or vice versa.
.TP
.B u | h
Text - files in the MacBinary format with nonempty data forks
and empty resource forks are made from ordinary data files, or vice versa.
The newly created MacBinary file has creator field given by
the MAC_EDITOR environment variable.
Option 'u', for usual, performs conversion.
Option 'h', for host, performs no conversion.
When converting,
Unix newline
characters are interchanged with MacIntosh carriage return
characters.
.PP
.SH "ENVIRONMENT VARIABLES"
There are four environment variables one may use to customize
the behavior of
.I mcvert
slightly.
.TP
.B MAC_EDITOR
The creator of MacBinary text files produced with options -uD.
The default is MACA, the creator type of MacWrite.
.TP
.B MAC_DLOAD_DIR
The MacBinary files created when option -D is selected are placed in this
directory. The default is ".", the current working directory.
.TP
.B MAC_EXT
The MacBinary files created when option -D is selected are named according
to the filename field stored in the file header, with the name extended by
this suffix. The default is ".bin".
.TP
.B MAC_LINE_LIMIT
The BinHex files created when option -U is selected may be no longer than
this many lines long. Files that would otherwise exceed this line limit
are broken up into several files with numbers embedded into their file
names to show their order. Each such file has "Start of part x" and "End
of part x" messages included where appropriate.
.SH BUGS
It should be possible to discard bad input now and successfully translate
good input later, but bad input mostly just causes immediate termination.
.PP
A more diligent person would support BinHex 3.0 and BinHex 2.0 and BinHex
5000.0 B. C., but I've never seen anyone use them in three years. A
more diligent person would also do something for users of macget and
macput, but hopefully someone will make those programs support the
MacBinary file protocol.
.PP
The rules for when files are suffixed with extentions
like .rsrc and .data are not obvious or are unreasonable.
For example, when converting from MacBinary to
resource, data, or text, the suffix is only appended if the non
suffixed version of the file is readable. This can lead to anomalies.
For example, if there is a file called foo.bin which is the MacBinary
representation for a file called foo, and there is no file named foo,
then the first mcvert -rU foo will create foo, yet subsequent mcvert -rU foo
requests will create foo.rsrc.
.SH SEE ALSO
xbin(1), macget(1), macput(1), xmodem(1), kermit(1)
.SH AUTHORS
Doug Moore, Cornell University Computer Science. Based upon
.I xbin
by Dave Johnson, Brown University, as modified by Guido van Rossum, and upon
.I unpit
by Allan G. Weber, as well as upon correspondence with several helpful
readers of USENET.
Joseph P. Skudlarek (Jskud@std.mentorg.com) made numerous maintenance releases.
Also, see the comments in mcvert.c for additional supporting characters.
SHAR_EOF
chmod 0444 mcvert.1 ||
echo 'restore of mcvert.1 failed'
Wc_c="`wc -c < 'mcvert.1'`"
test 7376 -eq "$Wc_c" ||
echo 'mcvert.1: original size 7376, current size' "$Wc_c"
fi
exit 0